package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.OrdersMapper;
import cn.scxy.bookcity.pojo.dto.OrdersAddNewDTO;
import cn.scxy.bookcity.pojo.dto.ShoppingCarAddNewDTO;
import cn.scxy.bookcity.pojo.entity.CarProduct;
import cn.scxy.bookcity.pojo.entity.Orders;
import cn.scxy.bookcity.pojo.vo.OrderVO;
import cn.scxy.bookcity.pojo.vo.OrdersListItemVO;
import cn.scxy.bookcity.pojo.vo.shoppingCarVO;
import cn.scxy.bookcity.service.IOrdersService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class OrdersServiceImpl implements IOrdersService {
    @Autowired
    OrdersMapper ordersMapper;

    @Override
    public List<OrderVO> list1(Long uid) {
        return ordersMapper.list1(uid);
    }

    @Override
    public void deleteById(Long id) {
        ordersMapper.deleteById(id);
    }

    @Override
    public void deleteByoId(Long oid) {
        ordersMapper.deleteByoId(oid);
    }

    @Override
    public void changeState(Integer id) {
        ordersMapper.changeState(id);
    }

    @Override
    public void updateStateIs3ByoId(Long oId) {
        ordersMapper.updateStateIs3ById(oId);
    }

    @Override
    public List<OrdersListItemVO> listByStateIs0(Long uId) {
        log.debug("开始处理查询待支付state为0的订单");
        List<OrdersListItemVO> list = ordersMapper.list(uId);
        List<OrdersListItemVO> state0List = new ArrayList<>();
        for (OrdersListItemVO itemVO : list) {
            if (itemVO.getState() == 0) {
                state0List.add(itemVO);
            }
        }
        log.debug("查询完成,{}",state0List);
        return state0List;
    }

    @Override
    public List<OrdersListItemVO> listByStateIs1(Long uId) {
        log.debug("开始处理查询待发货state为1的订单");
        List<OrdersListItemVO> list = ordersMapper.list(uId);
        List<OrdersListItemVO> state1List = new ArrayList<>();
        for (OrdersListItemVO itemVO : list) {
            if (itemVO.getState() == 1) {
                state1List.add(itemVO);
            }
        }
        return state1List;
    }

    @Override
    public List<OrdersListItemVO> listByStateIs2(Long uId) {
        log.debug("开始处理查询待发货state为2的订单");
        List<OrdersListItemVO> list = ordersMapper.list(uId);
        List<OrdersListItemVO> state2List = new ArrayList<>();
        for (OrdersListItemVO itemVO : list) {
            if (itemVO.getState() == 2) {
                state2List.add(itemVO);
            }
        }
        return state2List;
    }

    @Override
    public List<OrdersListItemVO> listByStateIs3(Long uId) {
        log.debug("开始处理查询已收货state为3的订单");
        List<OrdersListItemVO> list = ordersMapper.list(uId);
        List<OrdersListItemVO> state3List = new ArrayList<>();
        for (OrdersListItemVO itemVO : list) {
            if (itemVO.getState() == 3) {
                state3List.add(itemVO);
            }
        }
        return state3List;
    }

    @Override
    public Long insert(OrdersAddNewDTO ordersAddNewDTO) {
        log.debug("开始处理【添加订单】的业务，参数：{}", ordersAddNewDTO);
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersAddNewDTO, orders);
        orders.setMoney(ordersAddNewDTO.getAllMoney());
        log.debug("即将插入订单数据：{}", ordersAddNewDTO);
        Long rows = ordersMapper.insert(ordersAddNewDTO);
        System.out.println("----------"+ordersAddNewDTO.getId());
        if (rows != 1) {
            String message = "添加订单失败，服务器忙，请稍后再尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("插入订单数据完成");
        return ordersAddNewDTO.getId();
    }
}
